package com.xbongbong.crm.perform.query.detail;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.crm.abstracts.AbstractCrmDetailBaseInfoService;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.parent.parse.data.FormDataListAnalysisDataHelp;
import com.xbongbong.pro.constant.I18nStringConstant;
import com.xbongbong.pro.form.pojo.dto.FormConfigGetDTO;
import com.xbongbong.pro.formdata.pojo.dto.SaasAnalysisDataDTO;
import com.xbongbong.pro.formexplain.pojo.dto.HandlerExplainDTO;
import com.xbongbong.pro.formexplain.pojo.vo.HandlerExplainVO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.FormConfigEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ClueEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.enums.dictionary.ClueStatusEnum;
import com.xbongbong.saas.model.ClueModel;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.xbongbong.saas.toolbox.help.FastJsonHelper.getIntegerOrDefaultFromFormData;

/**
 * @author 吴峰
 * @date 2022/06/21 16:36
 */
@Service("clueDetailBaseInfoServiceImpl")
public class ClueDetailBaseInfoServiceImpl extends AbstractCrmDetailBaseInfoService {
    @Resource
    private ClueModel clueModel;
    @Resource
    private FormDataListAnalysisDataHelp formDataListAnalysisDataHelp;

    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.CLUE.getCode());
    }

    @Override
    public void checkPermission(PaasFormEntityExt formEntity, PaasFormDataEntityExt paasFormDataEntityExt, UserVO loginUser, Integer subBusinessType) throws XbbException {

    }

    @Override
    public void handleDetailSpecialExplain( PaasFormDataEntityExt paasFormDataEntityExt, List<FieldAttrEntity> explainList) throws XbbException{
        JSONObject data= paasFormDataEntityExt.getData();
        String status = FastJsonHelper.getStringOrDefaultFromFormData(data, ClueEnum.CLUE_STATUS.getAttr(), "");
        if (!Objects.equals(status, ClueStatusEnum.INVALID.getCode())) {
            explainList.removeIf(fieldAttrEntity -> Objects.equals(fieldAttrEntity.getAttr(), ClueEnum.INVALID_REASON.getAttr())
                    || Objects.equals(fieldAttrEntity.getAttr(), ClueEnum.INVALID_MEMO.getAttr()));
        }
        Integer publicFlag = getIntegerOrDefaultFromFormData(data, ClueEnum.IS_PUBLIC.getAttr(), 0);
        if (Objects.equals(publicFlag, 1)) {
            explainList.forEach(item -> {
                if (Objects.equals(item.getAttr(), ClueEnum.OWNER_ID.getAttr())) {
                    item.setAttrName(I18nMessageUtil.getMessage(I18nStringConstant.PRE_OWNER_ID));
                }
            });
        }
    }

    @Override
    public JSONObject analysisData(PaasFormDataEntityExt formData, HandlerExplainDTO handlerExplainDTO, HandlerExplainVO handlerExplainVO) throws XbbException {
        List<FieldAttrEntity> list = handlerExplainVO.getHeadList();
        Map<String, FieldAttrEntity> explainMap = new HashMap<>(list.size());
        list.forEach(item->explainMap.put(item.getAttr(), item));
        Integer publicFlag = getIntegerOrDefaultFromFormData(formData.getData(), ClueEnum.IS_PUBLIC.getAttr(), BasicConstant.ZERO);
        if (Objects.equals(publicFlag, BasicConstant.ONE)) {
            FieldAttrEntity cluePhone = explainMap.get(ClueEnum.PHONE.getAttr());
            if (Objects.nonNull(cluePhone)) {
                Integer showPublicPool = cluePhone.getShowPublicPool();
                Integer publicPool = cluePhone.getPublicPool();
                if (Objects.equals(showPublicPool, BasicConstant.ONE) && Objects.equals(publicPool, BasicConstant.ZERO)) {
                    // 如果模板电话字段配置了公海池不可见，那么公海客户的电话不可见
                    cluePhone.setVisible(BasicConstant.ZERO);
                }
            }
        }
        JSONObject data = commonAnalysisAllData(formData, handlerExplainDTO, handlerExplainVO, list, explainMap);
        SaasAnalysisDataDTO saasAnalysisDataDTO = new SaasAnalysisDataDTO();
        BeanUtil.copyProperties(handlerExplainDTO, saasAnalysisDataDTO);
        saasAnalysisDataDTO.setFormId(formData.getFormId());
        clueModel.formatData(saasAnalysisDataDTO, data);

        FormConfigGetDTO formConfigGetDTO = new FormConfigGetDTO();
        formConfigGetDTO.setCorpid(handlerExplainDTO.getCorpid());
        formConfigGetDTO.setBusinessType(XbbRefTypeEnum.CLUE.getCode());
        formConfigGetDTO.setFormId(formData.getFormId());
        Map<String, String> lastConnectTimeMap = formDataListAnalysisDataHelp.lastConnectTime(formConfigGetDTO);
        String lastConnectTime = lastConnectTimeMap.get(FormConfigEnum.CLUE_LAST_CONNECT_TIME.getConfig());
        Long clueLastTime = FastJsonHelper.getLongFromFormData(data, ClueEnum.LAST_CONNECT_TIME.getAttr());
        if(lastConnectTime != null){
            String time = Objects.equals(lastConnectTime, "1") ? DateTimeUtil.getLastConnectTimeStr(clueLastTime) : DateUtil.getString(clueLastTime * 1000L, DateUtil.SDFYMDHM);
            data.put(ClueEnum.LAST_CONNECT_TIME.getAttr(), time);
        }
        return data;
    }
}
